package util.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;

public class MultiProtocolFile_FS implements IMultiProtocolFile
{
	protected String path;
	protected File fsFile;
	
	public MultiProtocolFile_FS(String path) throws IOException
	{
		this.path = path;
		this.fsFile = new File(path);
	}
	
	public MultiProtocolFile_FS(File fsFile) throws IOException
	{
		this.fsFile = fsFile;
		this.path = fsFile.getPath();
	}	
	
	//
	// IMultiProtocolFile
	//
	
	@Override
	public InputStream getInputStream() throws IOException
	{
		return new FileInputStream(fsFile);
	}

	
	@Override
	public OutputStream getOutputStream() throws IOException
	{
		return getOutputStream(false);
	}
	
	@Override
	public OutputStream getOutputStream(boolean append) throws IOException
	{
		return new FileOutputStream(fsFile, append);
	}
	
	//
	// File Methods
	//
	
	@Override
	public boolean exists()
	{
		return fsFile.exists();
	}
	
	@Override
	public boolean canRead() throws IOException
	{
		return fsFile.canRead();
	}
	
	@Override
	public boolean canWrite() throws IOException
	{
		return fsFile.canWrite();
	}
	
	@Override
	public long lastModified() throws IOException
	{
		return fsFile.lastModified();
	}
	
	@Override
	public long length() throws IOException
	{
		return fsFile.length();
	}
	
	@Override
	public MultiProtocolFile[] listFiles() throws IOException
	{
		File[] lf = fsFile.listFiles();
		if (lf == null || lf.length < 1)
			return new MultiProtocolFile[0];
		
		MultiProtocolFile[] res = new MultiProtocolFile[lf.length];
		for (int i=0; i<lf.length; i++)
			res[i] = new MultiProtocolFile(lf[i]);
		
		return res;
	}
	
	@Override
	public boolean mkdirs()
	{
		return fsFile.mkdirs();
	}
	
	@Override
	public boolean createNewFile() throws IOException
	{
		return fsFile.createNewFile();
	}
	
	@Override
	public boolean delete()
	{
		return fsFile.delete();
	}
	
	@Override
	public boolean setLastModified(long time)
	{
		return fsFile.setLastModified(time);
	}
	
	@Override
	public long getLastModified()
	{
		return fsFile.lastModified();
	}
	
	@Override
	public boolean renameTo(IMultiProtocolFile dest) throws IOException
	{
		if (dest instanceof MultiProtocolFile)
			dest = ((MultiProtocolFile)dest).impl;
		
		if (dest instanceof MultiProtocolFile_FS)
		{
			return this.fsFile.renameTo(((MultiProtocolFile_FS)dest).fsFile);
		}
		
		throw new IOException("Unsupported Cross FS renameTo: " + this.toString() + " -> " + dest.toString());
	}

	@Override
	public String toString()
	{
		return path;
	}
	
	@Override
	public String getPath()
	{
		return fsFile.getAbsolutePath();
	}
	
	@Override
	public String getParent()
	{
		return fsFile.getParent();
	}
	
	@Override
	public String getName()
	{
		return fsFile.getName();
	}
	
	@Override
	public boolean isFile() throws IOException
	{
		return fsFile.isFile();
	}
	
	@Override
	public boolean isDirectory() throws IOException
	{
		return fsFile.isDirectory();
	}
	
}
